package com.pro.controller;

import com.pro.pojo.*;
import com.pro.service.JsGoodsService;
import com.pro.utils.FastDFSClientUtil;
import com.pro.utils.ImportExcelUtil;
import com.pro.utils.MultipartFileToFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author cyj
 * @date 2022.1.10$ 14.08$
 * @content :
 */

@RestController
@RequestMapping("/goodsTable")
public class JsGoodsController {
    @Autowired
    private JsGoodsService service;

    @Autowired
    private FastDFSClientUtil dfsClient;

    private String ExcelUrl;

    @RequestMapping("/getTable")
    public JsonData getTable(JsGoods jsProduct){
        List<JsGoods> table = service.getTable(jsProduct);
        int count = service.count(jsProduct);
        return table!=null?JsonData.buildSuccess(table,count):JsonData.buildError("服务端错误");
    }

    /*根据按钮修改上下架状态     2022.1.11   15.53     CYJ*/
    @RequestMapping("/updateGoodsSate")
    public JsonData updateGoodsSate(Integer productId,Integer productSate) {
        Boolean flag=service.updateGoodsSate(productId,productSate);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }

    /*查看某个商品的详细审核信息 2022.1.11   16.44     CYJ*/
    @RequestMapping("/getGoodsRecord")
    public JsonData getGoodsRecord(Integer productId) {
        List<JsCourseRecord> list=service.getGoodsRecord(productId);
        return list!=null?JsonData.buildSuccess(list):JsonData.buildError("服务端错误");
    }

    /*删除一个商品               2022.1.11   19.47     CYJ*/
    @RequestMapping("/delGoods")
    public JsonData delGoods(Integer productId) {
        Boolean flag=service.delGoods(productId);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }

    /*高级检索商品               2022.1.12   9.34     CYJ*/
    @RequestMapping("/SearchGoods")
    public JsonData SearchGoods(JsGoods jsGoods) {
        List<JsGoods> table = service.SearchGoods(jsGoods);
        return table!=null?JsonData.buildSuccess(table):JsonData.buildError("服务端错误");
    }

    /*查看一个商品的详细信息     2022.1.12   11.29     CYJ*/
    @RequestMapping("/getGoodsMsg")
    public JsonData getGoodsMsg(Integer productId) {
        JsGoods goods = service.getGoodsMsg(productId);
        return goods!=null?JsonData.buildSuccess(goods):JsonData.buildError("服务端错误");
    }

    /*修改一件商品的详细信息     2022.1.12   14.32     CYJ*/
    @RequestMapping("/updateGoods")
    public JsonData updateGoods(JsGoods jsGoods){
        Boolean flag = service.updateGoods(jsGoods);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }

    /*查看商品的日志             2022.1.12   16.28     CYJ*/
    @RequestMapping("/getGoodsJournal")
    public JsonData getGoodsJournal(Integer productId, Integer type){
        List<JsCourse> journal = service.getGoodsJournal(productId,type);
        return journal!=null?JsonData.buildSuccess(journal):JsonData.buildError("服务端错误");
    }

    /**
     * @author CYJ
     * @date 2022/1/13  18:50
     * @content : 查看商品的相关操作日志
     */
    @RequestMapping("/getGoodsJournals")
    public JsonData getGoodsJournals(String type,String executionTime,Integer operationType){
        List<JsOperationLog> JsOperationLog = service.getGoodsJournals(type,executionTime,operationType);
        return JsOperationLog!=null?JsonData.buildSuccess(JsOperationLog):JsonData.buildError("服务端错误");
    }
    /**
     * @author CYJ
     * @date 2022/1/13  19:45
     * @content : 删除一条日志
     */
    @RequestMapping("/delJournalById")
    public JsonData delJournalById(Integer id){
        Boolean flag = service.delJournalById(id);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }

    /**
     * @author CYJ
     * @date 2022/1/13  8.56
     * @content : 获取商品历史价格记录
     */
    @RequestMapping("/getPriceJournal")
    public JsonData getPriceJournal(Integer productCode,String  productName,String updateDate,Integer type){
        List<JsCourseJournal> priceJournal = service.getPriceJournal(productCode,productName,updateDate,type);
        return priceJournal!=null?JsonData.buildSuccess(priceJournal):JsonData.buildError("服务端错误");
    }
    /**
     *
     * @author CYJ
     * @date 2021/12/14 2002.9.26
     * @content :删除一条商品历史价格记录
     */
    @RequestMapping("/delPriceJournal")
    public JsonData delPriceJournal(Integer id){
        Boolean flag = service.delPriceJournal(id);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }

    /**
     *
     * @author CYJ
     * @date 2022/1.19  18.53
     * @content :获取商品的分类
     *
     */
    @RequestMapping("/getGoodsClass")
    public JsonData getGoodsClass(){
        List<JsGoodsCategroy> flag=service.getGoodsClass();
        return flag!=null?JsonData.buildSuccess(flag):JsonData.buildError("服务器错误");
    }
    /**
     *
     * @author CYJ
     * @date 2022/1.19  19.13
     * @content :添加商品
     */
    @RequestMapping("/addGoods")
    public JsonData addProduct(JsProduct jsProduct){
        jsProduct.setCreateDate(new Date());
        Boolean flag = service.addGoods(jsProduct);
        return flag!=false?JsonData.buildSuccess(jsProduct):JsonData.buildError("服务器错误");
    }
    /**
     *
     * @author CYJ
     * @date 2022/1.22  18.36
     * @content :获取审核商品信息
     */
    @RequestMapping("/getExamineTable")
    public JsonData getExamineTable(String productName,String title,String trueName,Integer type,String result){
        System.err.println(productName+"/"+title+"/"+trueName+"/"+type);
        List<JsGoods> list = service.getExamineTable(productName,title,trueName,type,result);
        return list!=null?JsonData.buildSuccess(list):JsonData.buildError("服务器错误");
    }

    @RequestMapping("/insertRecord")
    public JsonData insertRecord(JsCourseRecord jsCourseRecord,String time){
        String result = jsCourseRecord.getResult();
        if(result=="yes"){
            jsCourseRecord.setResult("1");
        }else if(result=="no"){
            jsCourseRecord.setResult("2");
        }
        System.err.println(jsCourseRecord.toString());
        System.err.println(jsCourseRecord.getCourseName());
        Boolean flag = service.insertRecord(jsCourseRecord);
        return flag!=false? JsonData.buildSuccess(flag): JsonData.buildError("服务端错误");
    }

    /**
     *
     * @author CYJ
     * @date 2022/1.28  20.39
     * @content :商品点击数
     */
    @RequestMapping("/hitsSum")
    public JsonData hitsSum(){
        List<JsGoods> goodsStatistics = service.hitsSum();
        return goodsStatistics!=null? JsonData.buildSuccess(goodsStatistics): JsonData.buildError("服务器错误");
    }

    /**
     *
     * @author CYJ
     * @date 2022/1.28  20.56
     * @content :商品收藏数
     */
    @RequestMapping("/collectionSum")
    public JsonData collectionSum(){
        List<JsGoods> goodsStatistics = service.collectionSum();
        return goodsStatistics!=null? JsonData.buildSuccess(goodsStatistics): JsonData.buildError("服务器错误");
    }

    //导入
//    @RequestMapping("/importExcel")
//    public JsonData importExcel(MultipartFile file, HttpSession session, HttpServletRequest request) throws Exception {
//
//        System.err.println("全局："+ExcelUrl);
//
//
//    }

    /*
     * @author CYJ
     * @date 2022/2.10  14.34
     * @content :导入文件
     */
    @RequestMapping("/uploadFile")
    public JsonData fdfsUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String resAccessUrl =null;
        try {
            String fileUrl = dfsClient.uploadFile(file);
            resAccessUrl = dfsClient.getResAccessUrl(fileUrl);
            int index=resAccessUrl.lastIndexOf(".");
            String str = resAccessUrl.substring(index,resAccessUrl.length());
            if(!str.equals(".xls") && !str.equals(".xlsx")){
                System.err.println("错误");
                return JsonData.buildError("请传入xls或xlsx文件");
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("IO异常");
        }
        String importStatus = "";
        HashMap<String, Object> map = new HashMap<>();
        MultipartFileToFileUtil toFile = new MultipartFileToFileUtil();
        File excel = null;
        try {
            excel = toFile.multipartFileToFile(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<List<Object>> excleDataLists= null;
        try {
            excleDataLists = ImportExcelUtil.readExcel(excel);
            System.err.println(excleDataLists);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Boolean flag = false;
        try{
            for (int i = 1; i < excleDataLists.size(); i++) {
                System.err.println("已执行循环导入");
                JsProduct good = new JsProduct();
                good.setProductCode(Integer.parseInt(excleDataLists.get(i).get(1).toString()));
                good.setCategroyId(Integer.parseInt(excleDataLists.get(i).get(2).toString()));
                good.setRecommend(Integer.parseInt(excleDataLists.get(i).get(3).toString()));
                good.setDiscount(Integer.parseInt(excleDataLists.get(i).get(4).toString()));
                good.setEnsure(Integer.parseInt(excleDataLists.get(i).get(5).toString()));
                good.setProductName(excleDataLists.get(i).get(6).toString());
                System.err.println(good);
                good.setSearchKeywords(excleDataLists.get(i).get(7).toString());
                good.setMainImage(excleDataLists.get(i).get(8).toString());
                good.setOriginalpriceProduct(Integer.parseInt(excleDataLists.get(i).get(5).toString()));
                good.setProductDescription(excleDataLists.get(i).get(10).toString());
                good.setProductSate(Integer.parseInt(excleDataLists.get(i).get(5).toString()));
                good.setCreateId(excleDataLists.get(i).get(12).toString());
                good.setProductLabel(excleDataLists.get(i).get(13).toString());
                flag = true;
                System.err.println(good);
                Boolean addFlag = service.addGoods(good);
                System.err.println(addFlag);
                if(addFlag!=true){
                    flag = false;
                }
            }
        }catch (Exception e){
            JsonData.buildSuccess("导入成功未进行添加,请检查文件内容格式是否正确");
        }

        if(flag){
            importStatus = "导入成功";
            map.put("importStatus",importStatus);
            return importStatus!="导入失败"?JsonData.buildSuccess(map):JsonData.buildError("服务器错误");
        }
        importStatus = "导入成功，未进行添加，请检查文件是否有内容或格式是否正确";
        map.put("importStatus",importStatus);
        return JsonData.buildSuccess(map);
    }
}
